package org.example.springblog.service;

import org.example.springblog.mapper.BlogInfoMapper;
import org.example.springblog.mapper.UserInfoMapper;
import org.example.springblog.model.BlogInfo;
import org.example.springblog.model.UserInfo;
import org.example.springblog.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/4
 * Time: 02:17
 */
@Service
public class UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public UserInfo selectByUserName(String userName) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        return userInfoMapper.selectByUserName(userInfo);
    }

    public UserInfo getLoginUserInfo(Integer userId) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        return userInfoMapper.selectById(userInfo);
    }

    public UserInfo getAuthorUserInfo(Integer blogId) {
        //获取用户id
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        BlogInfo resultBlog = blogInfoMapper.selectBlogInfoById(blogInfo);
        Integer userId = resultBlog.getUserId();
        //获取用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        return userInfoMapper.selectById(userInfo);
    }

    public Integer addBlogCountByUserId(Integer userId) {
        UserInfo loginUserInfo = this.getLoginUserInfo(userId);
        loginUserInfo.setBlogCount(loginUserInfo.getBlogCount() + 1);
        return userInfoMapper.addBlogCountByUserId(loginUserInfo);
    }

    public Integer subBlogCountByUserId(Integer userId) {
        UserInfo loginUserInfo = this.getLoginUserInfo(userId);
        loginUserInfo.setBlogCount(loginUserInfo.getBlogCount() - 1);
        return userInfoMapper.subBlogCountByUserId(loginUserInfo);
    }

    public Integer register(String userName, String password, String githubUrl) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setPassword(password);
        if (githubUrl != null) {
            userInfo.setGithubUrl(githubUrl);
        }
        return userInfoMapper.register(userInfo);
    }

    public Integer modify(Integer id,String userName, String password, String githubUrl){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setUserName(userName);
        userInfo.setPassword(password);
        if (githubUrl != null) {
            userInfo.setGithubUrl(githubUrl);
        }
        return userInfoMapper.modify(userInfo);
    }

    public List<UserInfo> getUserList() {
        List<UserInfo> userList = userInfoMapper.getUserList();
        for (UserInfo userInfo : userList) {
            userInfo.setCreateTimeStatus(DateUtils.dateFormat(userInfo.getCreateTime()));
            userInfo.setUpdateTimeStatus(DateUtils.dateFormat(userInfo.getUpdateTime()));
            if(userInfo.getPower() == 1){
                userInfo.setPowerStatus("管理员");
            }else{
                userInfo.setPowerStatus("普通用户");
            }
        }
        return userList;
    }
}
